package com.lsh.service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsh.common.util.PageSurport;
import com.lsh.dao.MenuDao;
import com.lsh.dao.mapper.TsysUserMapper;
import com.lsh.model.TsysMenu;
import com.lsh.model.TsysUser;

@Service("menuService")
public class MenuService extends BasicService {

	private final static Log LOG = LogFactory.getLog(TsysMenu.class);
	@Autowired
	MenuDao dao;
	@Autowired
	TsysUserMapper mapper;

	/**
	 * 插入数据
	 * 
	 * @param tsysMenu
	 * @return
	 */
	public int insert(TsysMenu tsysMenu) {
		return dao.insert("TsysMenu-insert", tsysMenu);
	}

	/**
	 * 根据ID获取记录
	 * 
	 * @param tsysMenu
	 * @return
	 */
	public TsysMenu getById(String no) {
		return dao.getById("TsysMenu-getById", no);
	}

	/**
	 * 删除记录
	 * 
	 * @param object
	 * @return
	 */
	public int delete(TsysMenu tsysMenu) {
		return dao.delete("TsysMenu-delete", tsysMenu);
	}

	/**
	 * 获取分页数据
	 * 
	 * @param map
	 * @param page
	 * @return
	 */
	public PageSurport getPage(Map map, PageSurport page) {
		return dao.selectPageByPage("TsysMenu-getPage",
				"TsysMenu-getPageCount", page, map);
	}

	/**
	 * 获取全部记录
	 * 
	 * @param orderBy
	 * @return
	 */
	public List getAll(String orderBy) {
		return dao.selectAll("TsysMenu-getAll", orderBy);
	}

	public void update(com.lsh.model.TsysMenu tsysMenu) {
		dao.update("TsysMenu-update", tsysMenu);
	}

	public List getMenuListByUser(TsysUser user) {
		String sql ;
		if (user.getUsertype() == 0) 
		{
			sql = "select t1.MenuId,t1.ParentID,t1.MenuName,t1.OrderID,t1.ImageURL,t1.LinkUrl,t1.Comments,t1.MenuType,t1.CreateTime,t1.OpenTarget,t1.MenuLevel,t1.iscomponent"
					+ " from tsys_Menu t1"
					+ " inner join tsys_Menu t2 on t1.ParentID = t2.MenuID " 
					+ " Where t1.MenuLevel=2 and t1.MenuType='SYS'"
					+ " Order By t2.orderid,t1.orderid";
		} 
		else 
		{
			sql = "select  MenuId,ParentID, MenuName,  OrderID, ImageURL, "
					+ "LinkUrl, Comments, MenuType, CreateTime, OpenTarget, "
					+ "MenuLevel,iscomponent from v_usermenu " + "Where userid ="
					+ user.getUserid() + " Order by porder,OrderID";
		}
		return null;
	}

	public HashSet<String> getRightsByUser(TsysUser user) {
		
		String sql = "select upper(rightcode) from v_userright Where userid="
				+ user.getUserid();
		if(user.getUsertype()==0)
		{//超级管理员取所有权限
			sql = "select  upper(rightcode) from tsys_right where righttype='SYS' and nodetype=1 ";
		}
				
//		List<String> list = loginDAO.findBySqlQuery(sql);
//		HashSet<String> set = new HashSet<String>(list);
//		return set;
		return null;
	}

	public TsysUser getUser(TsysUser user) {
		return mapper.getUser(user);
	}

}
